package com.photoeditor.demo.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	
	public String getMD5(byte[] source) {
		String result = null;
		char hexDigits[] = {     
		    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',  'e', 'f'}; 
		try{
			MessageDigest md = MessageDigest.getInstance( "MD5" );
		    md.update(source);
		    byte tmp[] = md.digest();
		                             
		    char str[] = new char[16 * 2];
		                                  
		    int k = 0;                    
		    for (int i = 0; i < 16; i++) {
		                                  
		     byte byte0 = tmp[i];         
		     str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 
		                                             
		     str[k++] = hexDigits[byte0 & 0xf];      
		    } 
		    result = new String(str);                

		   }catch( Exception e ){
			   e.printStackTrace();
		   }
		   return result;
		 }

	public static String getMD5Str(String str) {
		MessageDigest messageDigest = null;

		try {
			messageDigest = MessageDigest.getInstance("MD5");

			messageDigest.reset();

			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (messageDigest == null) {
		    return null;
		}

		byte[] byteArray = messageDigest.digest();

		StringBuffer md5StrBuff = new StringBuffer();

		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}

		// return md5StrBuff.substring(8, 24).toString().toUpperCase();
		return md5StrBuff.toString().toUpperCase();
	}

	/**
	 * 转换成小写
	 * @param str
	 * @return
	 */
	public static String getMD5StrToLowerCase(String str) {
		return getMD5Str(str).toLowerCase();
	}
}